package com.andy.beef.ui.main.adapter

import android.text.TextUtils
import com.andy.beef.R
import com.andy.module.util.AndyTimeUtil
import com.andy.beef.ui.main.bean.FarmCattleBean
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder



class FarmCattleListAdapter : BaseMultiItemQuickAdapter<FarmCattleBean, BaseViewHolder>(null) {

    companion object {
        const val TYPE_HEADER = 1
        const val TYPE_ITEM = 0
    }

    init {
        addItemType(TYPE_HEADER, R.layout.main_adapter_header_farm_cattle_list)
        addItemType(TYPE_ITEM, R.layout.main_adapter_farm_cattle_list)
    }

    override fun convert(holder: BaseViewHolder, item: FarmCattleBean) {
        if (holder.itemViewType == TYPE_HEADER) {
            holder.setText(R.id.tv_desc, item.cattleFenceName)
        } else {
            initItemView(holder, item)
        }
    }

    private fun initItemView(helper: BaseViewHolder, item: FarmCattleBean) {
        helper.setText(R.id.tv_cattle_id, context.getString(R.string.main_format_cattle_num, item.cattleEarTagNo))
        helper.setText(R.id.tv_cattle_category, item.varietyDesc)
        helper.setText(R.id.tv_cattle_gender, item.genderDesc)
        if (item.birthday > 0) {
            val month = AndyTimeUtil.getBetweenMonth(System.currentTimeMillis(), item.birthday)
            helper.setText(R.id.tv_month, context.getString(R.string.main_format_month_age) + month)
        } else {
            helper.setText(R.id.tv_month, "月龄：-")
        }
        if (!TextUtils.isEmpty(item.cowshedName)) {
            helper.setText(R.id.tv_shed, context.getString(R.string.main_format_shed, item.cowshedName))
        } else {
            helper.setText(R.id.tv_shed, context.getString(R.string.main_format_shed, "-"))
        }
        if (!TextUtils.isEmpty(item.varietyDesc)) {
            helper.setText(R.id.tv_variety, context.getString(R.string.main_format_feed_type, item.varietyDesc))
        } else {
            helper.setText(R.id.tv_variety, context.getString(R.string.main_format_feed_type, "-"))
        }
        if (!TextUtils.isEmpty(item.cattleFenceName)) {
            helper.setText(R.id.tv_fence, context.getString(R.string.main_format_cattle_fence, item.cattleFenceName))
        } else {
            helper.setText(R.id.tv_fence, context.getString(R.string.main_format_cattle_fence, "-"))
        }

        val position = helper.layoutPosition - headerLayoutCount
        val nextPosition = position + 1
        if (nextPosition < data.size - 1) {
            //当不是最后一个
            val nextGroupBean = data[nextPosition]
            if (nextGroupBean.itemType == TYPE_HEADER) {
                helper.setGone(R.id.divider_view, false)
            } else {
                helper.setGone(R.id.divider_view, true)
            }
        } else {
            helper.setGone(R.id.divider_view, true)
        }
    }
}





